﻿using Forum.Infrastructure;
using Forum.Model.Entity.System;
using Forum.Repository.Interface;
using SqlSugar;

namespace Forum.Repository
{
    internal class UserRepository : IUserRepository
    {
        public ISqlSugarClient Db { get; }

        public UserRepository(ISqlSugarClient sqlSugarClient)
        {
            Db = sqlSugarClient;
        }

        /// <summary>
        /// 选择数据库新增用户
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> AddUsers(List<SYS_USER> entities)
        {
            int num = 0;
            foreach (var en in entities)
            {
                num += await DbContext.MySql.Insertable(en).ExecuteCommandAsync();
            }

            return num;
        }

        /// <summary>
        /// 获取自增长序列的下一个值
        /// </summary>
        /// <param name="entityName">实体名</param>
        /// <returns></returns>
        public int GetIncrementId(string entityName)
        {
            var sql = $"select auto_increment FROM information_schema.tables where  table_schema='forum' and table_name='{entityName}'";
            return Db.Ado.SqlQuery<int>(sql).First();
        }
    }
}
